<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LoRA同步测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #1a1a1a;
            color: #ffffff;
        }
        .container {
            background: #2a2a2a;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        button:disabled {
            background: #666;
            cursor: not-allowed;
        }
        .result {
            background: #333;
            padding: 15px;
            border-radius: 4px;
            margin: 10px 0;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .error {
            background: #dc3545;
            color: white;
        }
        .success {
            background: #28a745;
            color: white;
        }
        .info {
            background: #17a2b8;
            color: white;
        }
    </style>
</head>
<body>
    <h1>🎯 LoRA同步功能测试</h1>
    
    <div class="container">
        <h2>1. 获取本地LoRA文件列表</h2>
        <button onclick="getLoraFiles()">获取本地LoRA文件</button>
        <div id="files-result" class="result"></div>
    </div>
    
    <div class="container">
        <h2>2. 获取数据库中的LoRA列表</h2>
        <button onclick="getLoras()">获取数据库LoRA列表</button>
        <div id="loras-result" class="result"></div>
    </div>
    
    <div class="container">
        <h2>3. 手动同步LoRA</h2>
        <button onclick="syncLoras()" id="syncBtn">同步LoRA</button>
        <div id="sync-result" class="result"></div>
    </div>

    <script>
        const API_BASE = '/api/xhh';
        
        async function getLoraFiles() {
            const resultDiv = document.getElementById('files-result');
            resultDiv.textContent = '正在获取本地LoRA文件...';
            resultDiv.className = 'result info';
            
            try {
                const response = await fetch(`${API_BASE}/lora-files`);
                const data = await response.json();
                
                if (data.success) {
                    resultDiv.textContent = `成功获取 ${data.count} 个本地LoRA文件:\n${JSON.stringify(data.files, null, 2)}`;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `获取失败: ${data.error}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `请求失败: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function getLoras() {
            const resultDiv = document.getElementById('loras-result');
            resultDiv.textContent = '正在获取数据库LoRA列表...';
            resultDiv.className = 'result info';
            
            try {
                const response = await fetch(`${API_BASE}/loras`);
                const data = await response.json();
                
                if (data.success) {
                    resultDiv.textContent = `成功获取 ${data.loras.length} 个数据库LoRA:\n${JSON.stringify(data.loras, null, 2)}`;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `获取失败: ${data.error}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `请求失败: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function syncLoras() {
            const resultDiv = document.getElementById('sync-result');
            const syncBtn = document.getElementById('syncBtn');
            
            syncBtn.disabled = true;
            syncBtn.textContent = '同步中...';
            resultDiv.textContent = '正在同步LoRA...';
            resultDiv.className = 'result info';
            
            try {
                // 首先获取本地LoRA文件列表
                const filesResponse = await fetch(`${API_BASE}/lora-files`);
                const filesData = await filesResponse.json();
                
                if (!filesData.success) {
                    throw new Error(`获取本地LoRA文件失败: ${filesData.error}`);
                }
                
                const loraList = filesData.files.map(file => ({
                    path: file.path,
                    name: file.name
                }));
                
                if (loraList.length === 0) {
                    resultDiv.textContent = '没有找到本地LoRA文件';
                    resultDiv.className = 'result error';
                    return;
                }
                
                // 调用同步接口
                const response = await fetch(`${API_BASE}/sync-loras`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        loras: loraList
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    const message = `同步成功!\n新增: ${data.synced_count} 个\n更新: ${data.updated_count} 个\n总计处理: ${data.total_processed} 个`;
                    resultDiv.textContent = message;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `同步失败: ${data.error}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `同步失败: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                syncBtn.disabled = false;
                syncBtn.textContent = '同步LoRA';
            }
        }
        
        // 页面加载时自动获取信息
        window.onload = function() {
            getLoraFiles();
            getLoras();
        };
    </script>
</body>
</html>
